﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Xml;
using LibraryResources = Sarnata.Common.Providers.BaseCamp.Properties.Resources;

namespace Sarnata.Common.Providers.BaseCamp
{
    public class BaseCampProject : BaseCampObject
    {
        private BaseCampConnection _bcc = null;
        private int _id = 0;
        private string _name = String.Empty;
        private DateTime _createdOn = DateTime.MinValue;
        private string _status = String.Empty;
        private DateTime _lastChangedOn = DateTime.MinValue;
        private int _companyId = 0;
        private string _companyName = String.Empty;
        private string _announcement = String.Empty;
        private string _startPage = String.Empty;
        private bool _showWhiteboards = false;
        private bool _showAnnouncement = false;

        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                if (_id == value)
                    return;
                _id = value;
            }
        }
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (_name == value)
                    return;
                _name = value;
            }
        }
        public DateTime CreatedOn
        {
            get
            {
                return _createdOn;
            }
            set
            {
                if (_createdOn == value)
                    return;
                _createdOn = value;
            }
        }
        public string Status
        {
            get
            {
                return _status;
            }
            set
            {
                if (_status == value)
                    return;
                _status = value;
            }
        }
        public DateTime LastChangedOn
        {
            get
            {
                return _lastChangedOn;
            }
            set
            {
                if (_lastChangedOn == value)
                    return;
                _lastChangedOn = value;
            }
        }
        public int CompanyId
        {
            get
            {
                return _companyId;
            }
            set
            {
                if (_companyId == value)
                    return;
                _companyId = value;
            }
        }
        public string CompanyName
        {
            get
            {
                return _companyName;
            }
            set
            {
                if (_companyName == value)
                    return;
                _companyName = value;
            }
        }
        public string Announcement
        {
            get
            {
                return _announcement;
            }
            set
            {
                if (_announcement == value)
                    return;
                _announcement = value;
            }
        }
        public string StartPage
        {
            get
            {
                return _startPage;
            }
            set
            {
                if (_startPage == value)
                    return;
                _startPage = value;
            }
        }
        public bool ShowWhiteboards
        {
            get
            {
                return _showWhiteboards;
            }
            set
            {
                if (_showWhiteboards == value)
                    return;
                _showWhiteboards = value;
            }
        }
        public bool ShowAnnouncement
        {
            get
            {
                return _showAnnouncement;
            }
            set
            {
                if (_showAnnouncement == value)
                    return;
                _showAnnouncement = value;
            }
        }

        public BaseCampProject(BaseCampConnection bcc)
        {
            _bcc = bcc;
        }

        public BaseCampProject(BaseCampConnection bcc, int projectId)
        {
            _bcc = bcc;
            Load(projectId);
        }

        public BaseCampProject(BaseCampConnection bcc, XmlNode xn)
        {
            _bcc = bcc;
            Load(xn);
        }

        public void Load(int projectId)
        {
            XmlDocument xmlDoc = _bcc.SendSecureGetRequest(String.Format(CultureInfo.InvariantCulture, LibraryResources.GetProjectCommand, projectId), String.Empty);
            XmlNode xn = xmlDoc.SelectSingleNode("project");
            if (xn is XmlNode)
                Load(xn);
        
        }
        
        public void Load(XmlNode xn)
        {
            try
            {
                if (!(xn is XmlNode)) return;
                _id = getNodeValue<int>(xn.SelectSingleNode("id"));
                _name = getNodeValue<string>(xn.SelectSingleNode("name"));
                _createdOn = getNodeValue<DateTime>(xn.SelectSingleNode("created-on"));
                _status = getNodeValue<string>(xn.SelectSingleNode("status"));
                _lastChangedOn = getNodeValue<DateTime>(xn.SelectSingleNode("last-changed-on"));
                _companyId = getNodeValue<int>(xn.SelectSingleNode("company/id"));
                _companyName = getNodeValue<string>(xn.SelectSingleNode("company/name"));
                _announcement = getNodeValue<string>(xn.SelectSingleNode("announcement"));
                _startPage = getNodeValue<string>(xn.SelectSingleNode("start-page"));
                _showWhiteboards = getNodeValue<bool>(xn.SelectSingleNode("show-whiteboards"));
                _showAnnouncement = getNodeValue<bool>(xn.SelectSingleNode("show-announcement"));
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }
        }

        public static BaseCampProjectCollection GetAllProjects(BaseCampConnection bcc)
        {
            BaseCampProjectCollection bcpc = new BaseCampProjectCollection();
            XmlDocument xmlDoc = bcc.SendSecureGetRequest(LibraryResources.GetAllProjectsCommand, String.Empty);
            XmlNodeList xnl = xmlDoc.SelectNodes("projects/project");
            for (int x=0; x<xnl.Count; x++)
            {
                BaseCampProject bcp = new BaseCampProject(bcc, xnl.Item(x));
                bcpc.Add(bcp);
            }
            return bcpc;
        }

    }

    public class BaseCampProjectCollection : BindingList<BaseCampProject>
    {
    }
}
